﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;

namespace Dummy
{
    public class BattlefieldGameInfo
    {
        public static char? PickedCharacter { get; private set; }
        public static bool IsCharAdded { get; private set; }
        public static string CurrentWord { get; private set; }
        private static BattlefieldButtonInfo bbi_LastAddedCharacterToCurrentWord;
        private static BattlefieldButtonInfo bbi_NewAddedCharacterToBattlefield;
        private static MainPage UI;
        private static SynchronizationContext Sync { get; set; }

        public delegate void SendingCurrentWordDelagate(int moveI, int moveJ, string IJchar, string word);
        public static event SendingCurrentWordDelagate SendCurrentWordEvent;

        public static void Init(MainPage _UI, SynchronizationContext _Sync)
        {
            UI = _UI;
            Sync = _Sync;
            BattlefieldButtonInfo.Init(UI.gr_battlefield);
            PickedButtonInfo.Init(UI);

            InitNamesAndScores();
            InitErrorMessage();

            //debug
            //InitNewGame("балда", true);
        }

        public static void SendUI(SendOrPostCallback sendcallback)
        {
            Sync.Send(sendcallback, null);
        }

        public static void SendUI(SendOrPostCallback sendcallback, object state)
        {
            Sync.Send(sendcallback, state);
        }

        private static void InitNamesAndScores()
        {
            UI.tb_info_yourName.Text = "";
            UI.tb_info_competitorName.Text = "";
            
            UI.tb_info_yourScore.Text = "";
            UI.tb_info_competitorScore.Text = "";
        }

        public static void InitErrorMessage()
        {
            UI.tb_error.Text = "";
        }

        public static void SetErrorMessage(string error)
        {
            UI.tb_error.Text = error;
        }

        public static void SetGamerName(bool isYourName, string gamerName)
        {
            if (isYourName)
                UI.tb_info_yourName.Text = gamerName;
            else
                UI.tb_info_competitorName.Text = gamerName;
        }

        public static void SetGamersScores(int yourScore, int competotorScore)
        {
            UI.tb_info_yourScore.Text = yourScore.ToString();
            UI.tb_info_competitorScore.Text = competotorScore.ToString();
        }

        public static void InitNewGame(string startWord, bool yourMove)
        {
            ClearPickChar();
            ClearBattlefield();
            SetGamersScores(0, 0);
            BattlefieldButtonInfo.SetStartWord(startWord);
            if (yourMove)
                TakeMove();
            else
                GiveMove();
        }

        public static void AddNewCharacterToBattlefield(BattlefieldButtonInfo _info)
        {
            IsCharAdded = true;
            bbi_NewAddedCharacterToBattlefield = _info;
        }

        public static void PickCharacter(char _char)
        {
            PickedCharacter = _char;
        }

        private static void ClearPickChar()
        {
            PickedCharacter = null;
            PickedButtonInfo.UncolourLastPickedChar();
        }

        private static void DropAddedAndNewCharacter(bool deleteNewAddedCharacter)
        {
            bbi_LastAddedCharacterToCurrentWord = null;
            if ((bbi_NewAddedCharacterToBattlefield != null) && (deleteNewAddedCharacter))
                bbi_NewAddedCharacterToBattlefield.SetCharacter(null);
            bbi_NewAddedCharacterToBattlefield = null;
        }

        private static void InitCurrentWordButtons()
        {
            UI.bt_clearCurrentWord.Click -= ClearCurrentWord_Click;
            UI.bt_clearCurrentWord.Click += ClearCurrentWord_Click;

            UI.bt_sendCurrentWord.Click -= SendCurrentWord_Click;
            UI.bt_sendCurrentWord.Click += SendCurrentWord_Click;

            CloseClearAndSendButton();
        }

        private static void ClearBattlefield()
        {
            InitCurrentWord();
            InitCurrentWordButtons();

            ClearAddedCharAndDropAddedFlags(true);
        }

        private static void ClearAddedCharAndDropAddedFlags(bool deleteNewAddedCharacter)
        {
            IsCharAdded = false;
            DropAddedAndNewCharacter(deleteNewAddedCharacter);
            BattlefieldButtonInfo.DropIsAddedFlag();
        }

        private static void ClearCurrentWord_Click(object sender, RoutedEventArgs e)
        {
            ClearBattlefield();
        }

        private static void SendCurrentWord_Click(object sender, RoutedEventArgs e)
        {
            StartSendingCurrentWord();
        }

        private static void SwitchBattlefieldAndPickedButtons(bool isEnabled)
        {
            BattlefieldButtonInfo.SwitchButtons(isEnabled);
            PickedButtonInfo.SwitchButtons(isEnabled);
        }

        private static void StartSendingCurrentWord()
        {
            SwitchBattlefieldAndPickedButtons(false);
            CloseClearAndSendButton();
            if (SendCurrentWordEvent != null)
                SendCurrentWordEvent(bbi_NewAddedCharacterToBattlefield.i, 
                                     bbi_NewAddedCharacterToBattlefield.j, 
                                     bbi_NewAddedCharacterToBattlefield.character.Value+"",
                                     CurrentWord);
        }

        public static void EndSendingCurrentWord(bool ok)
        {
            if (!ok)
            {
                SwitchBattlefieldAndPickedButtons(true);
                ShowClearButton();
                ShowSendButton();
            }
            else
                EndMove();
        }

        private static void EndMove()
        {
            ClearPickChar();
            ClearAddedCharAndDropAddedFlags(false);
        }

        public static void GiveMove()
        {
            SwitchBattlefieldAndPickedButtons(false);
            InitCurrentWord();
            ClearAddedCharAndDropAddedFlags(false);
        }

        public static void TakeMove()
        {
            SwitchBattlefieldAndPickedButtons(true);
            InitCurrentWord();
            ClearAddedCharAndDropAddedFlags(false);
        }

        private static void InitCurrentWord()
        {            
            UI.tb_currentWord.Text = "";
            CurrentWord = "";
        }

        public static void ShowClearButton()
        {
            UI.bt_clearCurrentWord.Visibility = Visibility.Visible;
        }

        public static void ShowSendButton()
        {
            UI.bt_sendCurrentWord.Visibility = Visibility.Visible;
        }

        public static void CloseClearAndSendButton()
        {
            UI.bt_clearCurrentWord.Visibility = Visibility.Collapsed;
            UI.bt_sendCurrentWord.Visibility = Visibility.Collapsed;
        }

        public static void AddCharacterToCurrentWord(BattlefieldButtonInfo _info)
        {
            if (_info.character.HasValue)
            {
                if ((bbi_LastAddedCharacterToCurrentWord == null) ||
                    (bbi_LastAddedCharacterToCurrentWord != null && CanAddNewCharacterToCurrentWord(_info)))
                {
                    CurrentWord += _info.character.Value;
                    _info.isAdded = true;
                    bbi_LastAddedCharacterToCurrentWord = _info;
                    UI.tb_currentWord.Text = CurrentWord;
                }
                else
                {
                    //error
                }
            }
        }

        private static bool CanAddNewCharacterToCurrentWord(BattlefieldButtonInfo newInfo)
        {
            if (newInfo.isAdded)
                return false;
            if ((newInfo.i == bbi_LastAddedCharacterToCurrentWord.i)
                    && (Math.Abs(newInfo.j - bbi_LastAddedCharacterToCurrentWord.j) == 1))
                return true;
            else if ((newInfo.j == bbi_LastAddedCharacterToCurrentWord.j)
                    && (Math.Abs(newInfo.i - bbi_LastAddedCharacterToCurrentWord.i) == 1))
                return true;
            return false;
        }

        public static void AddCharacterToBattlefield(int moveI, int moveJ, char IJchar)
        {
            BattlefieldButtonInfo.AddCharacterToBattlefield(moveI, moveJ, IJchar);
        }

        public static void AddWordToList(bool isYourMove, string word)
        {
            if (isYourMove)
                UI.lb_info_yourWordList.Items.Add(word);
            else
                UI.lb_info_competitorWordList.Items.Add(word);
        }
    }
}
